<template>
  <div>
    <el-button type="primary" :icon="Plus" :disabled="!category3Id" @click="addSpuBtn"
      >添加Spu</el-button
    >
    <el-table :data="spuList" border style="width: 100%">
      <el-table-column type="index" label="序号" width="180" />
      <el-table-column prop="spuName" label="SPU名称" width="180" />
      <el-table-column prop="description" label="SPU介绍" width="180" />
      <el-table-column prop="address" label="操作">
        <template #default="{ row }">
          <el-button type="success" :icon="Plus" plain round @click="addSkuBtn"></el-button>
          <el-button type="warning" :icon="Edit" plain round @click="editSpuBtn(row)"></el-button>
          <el-button type="info" :icon="InfoFilled" plain round></el-button>
          <el-button
            type="danger"
            :icon="DeleteFilled"
            plain
            round
            @click="delSpuOne(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page.pageNow"
      v-model:page-size="page.limit"
      :page-sizes="[5, 10, 15, 20]"
      layout=" prev, pager, next, jumper, sizes,total"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { Plus, Edit, InfoFilled, DeleteFilled } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useCategoryStore } from '@/stores/category'
import { storeToRefs } from 'pinia'
import type { IResSpuList, IResSpu } from '@/types/spu'
import { reqDelSpu, reqSpuList } from '@/api/Spu'
import { ElMessage, ElMessageBox } from 'element-plus'

const spuList = ref<IResSpu[]>([])
const page = ref({
  pageNow: 1,
  limit: 10,
  total: 0
})
//三级列表实例化
const categoryStore = useCategoryStore()
const { category3Id } = storeToRefs(categoryStore)
//获取spu列表
const getSpuList = async () => {
  if (category3Id.value) {
    const res = await reqSpuList(page.value.pageNow, page.value.limit, category3Id.value)
    // console.log(res)

    if (res.code === 200) {
      console.log(res.data)
      page.value.total = res.data.total
      spuList.value = res.data.records
    }
  }
}
//删除单个spu
const delSpuOne = (row: IResSpu) => {
  ElMessageBox.confirm(`确定删除${row.spuName}?`, '删除提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      if (row.id) {
        const res = await reqDelSpu(row.id)
        if (res.code === 200) {
          ElMessage.success('删除成功')
          getSpuList()
        }
      }
    })
    .catch(() => {
      ElMessage.info('取消删除')
    })
}

//监听三级类型id
watch(
  category3Id,
  async (newVal) => {
    if (newVal) {
      getSpuList()
    } else {
      spuList.value = []
    }
  },
  {
    immediate: true
  }
)
/* watch(
  page,
  async (newVal) => {
    if (category3Id) {
      const res = await reqSpuList(newVal.pageNow, newVal.limit, category3Id.value)
      if (res.code === 200) {
        spuList.value = res.data.records
      }
    }
  },
  { immediate: true, deep: true }
) */

//自定义事件
const emit = defineEmits<{
  (e: 'setState', value: number, rwo?: IResSpu): void
}>()

//点击添加spu
const addSpuBtn = () => {
  emit('setState', 3)
}
//点击添加sku
const addSkuBtn = () => {
  emit('setState', 2)
}
//点击修改Spu
const editSpuBtn = (row: IResSpu) => {
  emit('setState', 3, row)
}

// 每页展示数量发生变化时触发
const handleSizeChange = (val: number) => {
  // console.log(`${val} items per page`)
  page.value.limit = val
  getSpuList()
}

// 页码发生改变时触发
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  page.value.pageNow = val
  getSpuList()
}
</script>

<script lang="ts">
export default {
  name: 'SpuList'
}
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}

.el-pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
